'use strict';

Object.defineProperty(exports, "__esModule", {
  value: true
});

var _defineProperty2 = require('babel-runtime/helpers/defineProperty');

var _defineProperty3 = _interopRequireDefault(_defineProperty2);

var _classCallCheck2 = require('babel-runtime/helpers/classCallCheck');

var _classCallCheck3 = _interopRequireDefault(_classCallCheck2);

var _possibleConstructorReturn2 = require('babel-runtime/helpers/possibleConstructorReturn');

var _possibleConstructorReturn3 = _interopRequireDefault(_possibleConstructorReturn2);

var _createClass2 = require('babel-runtime/helpers/createClass');

var _createClass3 = _interopRequireDefault(_createClass2);

var _inherits2 = require('babel-runtime/helpers/inherits');

var _inherits3 = _interopRequireDefault(_inherits2);

var _react = require('react');

var _react2 = _interopRequireDefault(_react);

var _codemirror = require('codemirror');

var _codemirror2 = _interopRequireDefault(_codemirror);

var _classnames2 = require('classnames');

var _classnames3 = _interopRequireDefault(_classnames2);

var _propTypes = require('prop-types');

var _propTypes2 = _interopRequireDefault(_propTypes);

var _reactLifecyclesCompat = require('react-lifecycles-compat');

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { 'default': obj }; }

function noop() {}

var RcCodeMirror = function (_React$Component) {
  (0, _inherits3['default'])(RcCodeMirror, _React$Component);
  (0, _createClass3['default'])(RcCodeMirror, null, [{
    key: 'getDerivedStateFromProps',
    value: function getDerivedStateFromProps(props, _ref) {
      var prevProps = _ref.prevProps,
          $self = _ref.$self;

      var nextState = {
        prevProps: props
      };
      if (prevProps && props.value && props.value !== $self.editor.getValue()) {
        $self.updated = true;
        $self.editor.setValue(props.value);
      }
      return nextState;
    }
  }]);

  function RcCodeMirror(props) {
    (0, _classCallCheck3['default'])(this, RcCodeMirror);

    var _this = (0, _possibleConstructorReturn3['default'])(this, (RcCodeMirror.__proto__ || Object.getPrototypeOf(RcCodeMirror)).call(this, props));

    _this.state = {
      $self: _this
    };
    return _this;
  }

  (0, _createClass3['default'])(RcCodeMirror, [{
    key: 'componentDidMount',
    value: function componentDidMount() {
      var _this2 = this;

      this.editor = (0, _codemirror2['default'])(this.ref);
      this.editor.on('change', function (_, metadata) {
        if (_this2.props.onChange && !_this2.updated) {
          _this2.props.onChange(_this2.editor, metadata, _this2.editor.getValue());
        }
      });
      this.editor.on('blur', function (_, metadata) {
        if (_this2.props.onBlur) {
          _this2.props.onBlur(_this2.editor, metadata, _this2.editor.getValue());
        }
      });
      if (this.props.onKeyUp) {
        this.editor.on('keyup', function (_, event) {
          _this2.props.onKeyUp(_this2.editor, event);
        });
      }
      this.editor.on('keydown', function (_, event) {
        _this2.updated = false;
        _this2.props.onKeyDown(_this2.editor, event);
      });
      Object.keys(this.props.options).forEach(function (key) {
        _this2.editor.setOption(key, _this2.props.options[key]);
      });
      this.editor.setValue(this.props.value);
    }
  }, {
    key: 'render',
    value: function render() {
      var _this3 = this;

      var className = (0, _classnames3['default'])((0, _defineProperty3['default'])({
        'react-codemirror': true
      }, this.props.className, !!this.props.className));
      return _react2['default'].createElement('div', { className: className, ref: function ref(self) {
          _this3.ref = self;
        } });
    }
  }]);
  return RcCodeMirror;
}(_react2['default'].Component);

RcCodeMirror.propTypes = {
  className: _propTypes2['default'].string,
  options: _propTypes2['default'].object,
  value: _propTypes2['default'].string,
  onChange: _propTypes2['default'].func,
  onKeyDown: _propTypes2['default'].func,
  onKeyUp: _propTypes2['default'].func,
  onBlur: _propTypes2['default'].func
};
RcCodeMirror.defaultProps = {
  options: {},
  value: '',
  onChange: noop,
  onKeyDown: noop,
  onKeyUp: noop
};
exports['default'] = (0, _reactLifecyclesCompat.polyfill)(RcCodeMirror);
module.exports = exports['default'];